package com.scalar.chiptrack.adaptor.utac;

import com.scalar.chiptrack.jobs.JobHandler;
import com.scalar.chiptrack.adaptor.AdaptorConstants;
import com.scalar.chiptrack.adaptor.AdaptorUtils;
import com.scalar.chiptrack.adaptor.InventoryLot;
import com.scalar.chiptrack.adaptor.AssemblyLot;
import com.scalar.chiptrack.adaptor.TestLot;
import com.scalar.chiptrack.adaptor.FabSortLot;
import com.scalar.chiptrack.adaptor.dao.InventoryLotDAO;
import com.scalar.chiptrack.adaptor.dao.AssemblyAdaptorDAO;
import com.scalar.chiptrack.adaptor.dao.TestAdaptorDAO;
import com.scalar.chiptrack.adaptor.dao.AdaptorRunInfo;
import com.scalar.chiptrack.adaptor.dao.AdaptorDAO;
import com.scalar.chiptrack.adaptor.dao.NewAdaptorDAO;
import com.scalar.chiptrack.adaptor.dao.FabSortAdaptorDAO;
import com.scalar.chiptrack.utils.Logger;
import com.scalar.chiptrack.utils.StringUtils;
import com.scalar.chiptrack.db.DBTransaction;
import com.scalar.ScalarException;

import java.util.HashMap;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Calendar;
import java.io.File;

import jxl.Workbook;
import jxl.Cell;
import jxl.Sheet;
import jxl.CellType;

/**
 * Created by IntelliJ IDEA.
 * User: Sudhakar
 * Date: Jul 3, 2007
 * Time: 6:24:42 PM
 */
public class UTACPOP3DataParser extends JobHandler implements AdaptorConstants
{

    private static Logger m_parserLogger    = null;
    private String m_dataOutputDir          = null;

    private HashMap m_assyWipDataMap        = new HashMap();
    private HashMap m_ftDataMap             = new HashMap();
    private HashMap m_dieBankDataMap        = new HashMap();
    private HashMap m_waferBankDataMap      = new HashMap();
    private HashMap m_sortDataMap           = new HashMap();
    private HashMap m_markPackDataMap       = new HashMap();


    public UTACPOP3DataParser() throws ScalarException
    {
        try
        {

            m_dataOutputDir = AdaptorUtils.getDataOutputDir( VENDOR_NAME_UTAC_SING, WIP_TYPE_ASSEMBLY );
            AdaptorUtils.makeAllAdaptorDirs( m_dataOutputDir );
            m_parserLogger = AdaptorUtils.getDataParserLogger( m_dataOutputDir,VENDOR_NAME_UTAC_SING );
            AssemblyAdaptorDAO.setLogger( m_parserLogger );   // All the DAOs and Parser should be logging into same file
            TestAdaptorDAO.setLogger( m_parserLogger);
            InventoryLotDAO.setLogger( m_parserLogger );
            FabSortAdaptorDAO.setLogger( m_parserLogger );
            AdaptorUtils.setLogger( m_parserLogger );
        }
        catch (ScalarException se)
        {
            log("ERROR!! ERROR!! ERROR!! Exception while loading/creating UTAC SING output directory from database, Exception: " + se);
            throw se;
        }
    }
    /**
     *
     * @param args
     * @throws Exception
     */
    public static void main( String args[] ) throws Exception
    {
        try
        {
            new UTACPOP3DataParser().execute();
        }
        catch (Exception e)
        {
            throw e;
        }
    }
    //Execute Method
   public void execute() throws Exception
   {
       AdaptorUtils.printHeader(VENDOR_NAME_UTAC_SING, CONSTANT_PARSER );

       File doneDestDir = new File( m_dataOutputDir + "/done" );

       File wipDataoutDir = new File( m_dataOutputDir );

       File[] fileList = wipDataoutDir.listFiles();

       if ( ( fileList == null ) || ( fileList.length <= 3 ) )
       {
           log( "No file found in directory: " + wipDataoutDir );
           throw new ScalarException( "No file found in directory: " + wipDataoutDir + "----\n\n" );
       }

       DBTransaction dbTransaction = null;
       ArrayList successFileList   = new ArrayList();
       boolean success             = false;

       try
       {
           for ( int i = 0; i < fileList.length; i++ )
           {
               if ( fileList[i].isDirectory() == true )
               {
                   continue;
               }
                              
               if ( ( fileList[i].getName() != null ) && ( fileList[i].getName().trim().indexOf( UTACPOP3Adaptor.FILE_NAME_WIP_STATUS ) != -1 ) )
               {
                    ArrayList utacWipList = parseUTACWipReport( fileList[i].getAbsolutePath() );
                   
                   if ( utacWipList != null )
                   {
                       getSheetDetails( utacWipList );
                   }
                   successFileList.add( fileList[i] );
               }
           }
       }
       catch ( Exception e )
       {
           success = false;
           log( "ERROR!! ERROR!! ERROR!! While parsing and storing File " + e );
           e.printStackTrace();
           throw e;
       }

        // to save the Wafer Store lot tables
       try
       {
           if ( m_waferBankDataMap.isEmpty() == false )
           {
               log(" Updating Wafer Store Lot Information");
               InventoryLotDAO.saveInventoryLot( dbTransaction/*null*/, m_waferBankDataMap.values().iterator(), INVENTORY_TYPE_WAFER_STORE, VENDOR_NAME_UTAC_SING );
               success = true;
           }
           else
           {
               InventoryLotDAO.closeInventoryLot(dbTransaction/*null*/, INVENTORY_TYPE_WAFER_STORE, VENDOR_NAME_UTAC_SING  );
           }

      }
      catch (ScalarException se)
      {
          success = false;
          se.printStackTrace();
          log("ERROR!! ERROR!! ERROR!! Could not update Wafer Store Lot information into database: " + se);
          throw se;
      }

        // to save the SORT lot tables
       try
       {
           if ( m_sortDataMap.isEmpty() == false )
           {
               log( " Updating Sort Lot Information" );
               FabSortAdaptorDAO.saveSortLot( null, m_sortDataMap.values().iterator(), WIP_TYPE_SORT, VENDOR_NAME_UTAC_SING );
               success = true;
           }
           else
           {
               FabSortAdaptorDAO.closeFabSortLot( dbTransaction, WIP_TYPE_SORT, VENDOR_NAME_UTAC_SING );
           }
      }
      catch (ScalarException se)
      {
          success = false;
          se.printStackTrace();
          log("ERROR!! ERROR!! ERROR!! Could not update Sort Lot information into database: " + se);
          throw se;
      }
       // to save the Die Bank lot tables
       try
       {
           if ( m_dieBankDataMap.isEmpty() == false )
           {
               log(" Updating Die Bank Lot Information");
               InventoryLotDAO.saveInventoryLot( dbTransaction/*null*/, m_dieBankDataMap.values().iterator(), INVENTORY_TYPE_DIE_BANK, VENDOR_NAME_UTAC_SING );
               success = true;
           }
           else
           {
               InventoryLotDAO.closeInventoryLot(dbTransaction/*null*/, INVENTORY_TYPE_DIE_BANK, VENDOR_NAME_UTAC_SING  );
           }
      }
      catch (ScalarException se)
      {
          success = false;
          se.printStackTrace();
          log("ERROR!! ERROR!! ERROR!! Could not update Assembly Lot information into database: " + se);
          throw se;
      }

       // to save the Assembly lot tables
       try
       {
           if ( m_assyWipDataMap.isEmpty() == false )
           {
               log(" Updating Assembly Lot Information");
               AssemblyAdaptorDAO.saveAssemblyLot( dbTransaction/*null*/, m_assyWipDataMap.values().iterator(), WIP_TYPE_ASSEMBLY, VENDOR_NAME_UTAC_SING );
               success = true;
           }
           else
           {
               AssemblyAdaptorDAO.closeAssemblyLot(dbTransaction, WIP_TYPE_ASSEMBLY, VENDOR_NAME_UTAC_SING );
           }
      }
      catch (ScalarException se)
      {
          success = false;
          se.printStackTrace();
          log("ERROR!! ERROR!! ERROR!! Could not update Assembly Lot information into database: " + se);
          throw se;
      }

      /*Updating the Test Lot Information*/
      try
      {
          // Save Test Information
          if( m_ftDataMap.isEmpty() == false )
          {                 
              log(" Updating Test Lot Information");
              TestAdaptorDAO.saveTestLot( dbTransaction/*null*/, m_ftDataMap.values().iterator(), LOCATION_TYPE_FT, VENDOR_NAME_UTAC_SING );
              success = true;
          }
          else
          {
              TestAdaptorDAO.closeTestLot( dbTransaction, LOCATION_TYPE_FT, VENDOR_NAME_UTAC_SING );
          }
      }
      catch (ScalarException se)
      {
          success = false;
          se.printStackTrace();
          log("ERROR!! ERROR!! ERROR!! Could not update Test Lot information into database: " + se);
          throw se;
      }
       // to save the Mark & Pack lot tables
       try
       {
           if ( m_markPackDataMap.isEmpty() == false )
           {
               log(" Updating Mark & Pack Lot Information");
               InventoryLotDAO.saveInventoryLot( dbTransaction/*null*/, m_markPackDataMap.values().iterator(), LOCATION_TYPE_BAKE_DRY, VENDOR_NAME_UTAC_SING );
               success = true;
           }
           else
           {
               InventoryLotDAO.closeInventoryLot(dbTransaction/*null*/, LOCATION_TYPE_BAKE_DRY, VENDOR_NAME_UTAC_SING  );
           }
      }
      catch (ScalarException se)
      {
          success = false;
          se.printStackTrace();
          log("ERROR!! ERROR!! ERROR!! Could not update Mark & Pack Lot information into database: " + se);
          throw se;
      }

      // TO SEND THE NEW LOTS, NEW TRANSIT LOTS, NO PO LOTS, DISCREPANCY LOTS NOTIFICATIONS TO THE USER
      AdaptorDAO.sendAllNotifications( VENDOR_NAME_UTAC_SING, true, true, true );

      // To Close the Drop Out Lots
      AdaptorDAO.closeDropOutLots( VENDOR_NAME_UTAC_SING );

      int adaptor_id = AdaptorRunInfo.getLastRunAdaptorId( UTACPOP3Adaptor.ADAPTOR_NAME, CONSTANT_ADAPTOR);

      // Update Adaptor_Run_Info table
      try
      {
            if ( success == true )
            {
                AdaptorRunInfo.saveAdaptorLastUpdatedStatus(CONSTANT_PARSER, UTACPOP3Adaptor.ADAPTOR_NAME, CONSTANT_SUCCESS, adaptor_id);
                System.out.println( "SUCCESSFULLY UPDATED IN THE DATABASE" );
            }
      }
      catch ( ScalarException se )
      {
            success = false; //As filES DATA SAVED , UPDATEING AS SUCCESS
            AdaptorRunInfo.saveAdaptorLastUpdatedStatus(CONSTANT_PARSER, UTACPOP3Adaptor.ADAPTOR_NAME, CONSTANT_SUCCESS, adaptor_id);
            log( "WARNING!! WARNING!! WARNING!! Could not update Adaptor run information, Exception: " + se );
            throw se;
       }
        // Move the successful files to 'done' directory
       if ( success )
       {
           AdaptorUtils.moveFilesToDone( successFileList, doneDestDir );
       }
   }//end of execute method


    /**
     *
     * @param fileName
     * @return
     * @throws Exception
     */
    private ArrayList parseUTACWipReport(String fileName) throws Exception
    {
        if (fileName == null) throw new ScalarException("Filename argument is null");

        UTACPOP3WipStatus utacpop3WipStatus = null;
        ArrayList utacWipStatusList = new ArrayList();
        String lotNocheckValue;
        Sheet sheet;
        try
        {
            File lotstatusFile = new File(fileName);
            if (lotstatusFile.exists() == false)
            {
                throw new ScalarException(fileName + " doesnt exist");
            }

            log("\nFound " + fileName);
            log("Parsing the file for UTAC WIP Status Report...");
            System.out.println("\nFound " + fileName);
            System.out.println("Parsing the file for UTAC WIP Status Report...");

            Workbook workbook = Workbook.getWorkbook(lotstatusFile);

            for (int sheetIndex = 0; sheetIndex < workbook.getNumberOfSheets(); sheetIndex++)
            {
                sheet = workbook.getSheet(sheetIndex);

                Cell[] row = null;

                for (int rowCount = 5; rowCount < sheet.getRows(); rowCount++)
                {
                    row = sheet.getRow(rowCount);

                    utacpop3WipStatus = new UTACPOP3WipStatus();

                    lotNocheckValue = row[0].getContents().trim();

                    if( lotNocheckValue != null && lotNocheckValue.trim().length() > 0 )
                    {
                        if (row.length > 0)
                        {
                            if (row[0].getType().equals(CellType.EMPTY))
                            {
                                utacpop3WipStatus.setCustomerDevice(null);
                            }
                            else
                            {
                                utacpop3WipStatus.setCustomerDevice(row[0].getContents().trim());
                            }
                        }
                        if (row.length > 1)
                        {
                            if (row[1].getType().equals(CellType.EMPTY))
                            {
                                utacpop3WipStatus.setCustPartIn(null);
                            }
                            else
                            {
                                utacpop3WipStatus.setCustPartIn(row[1].getContents().trim());
                            }
                        }
                        if (row.length > 2)
                        {
                            if (row[2].getType().equals(CellType.EMPTY))
                            {
                                utacpop3WipStatus.setCustPartOut(null);
                            }
                            else
                            {
                                utacpop3WipStatus.setCustPartOut(row[2].getContents().trim());
                            }
                        }
                        if (row.length > 3)
                        {
                            if (row[3].getType().equals(CellType.EMPTY))
                            {
                                utacpop3WipStatus.setStage(null);
                            }
                            else
                            {
                                utacpop3WipStatus.setStage(row[3].getContents().trim());
                            }
                        }
                        if (row.length > 4)
                        {
                            if (row[4].getType().equals(CellType.EMPTY))
                            {
                                utacpop3WipStatus.setStageDesc(null);
                            }
                            else
                            {
                                utacpop3WipStatus.setStageDesc(row[4].getContents().trim());
                            }
                        }
                        if (row.length > 5)
                        {
                            if (row[5].getType().equals(CellType.EMPTY))
                            {
                                utacpop3WipStatus.setCustomerLotId(null);
                            }
                            else
                            {
                                utacpop3WipStatus.setCustomerLotId(row[5].getContents().trim());
                            }
                        }
                        if (row.length > 6)
                        {
                            if (row[6].getType().equals(CellType.EMPTY))
                            {
                                utacpop3WipStatus.setUtacLotId(null);
                            }
                            else
                            {
                                utacpop3WipStatus.setUtacLotId(row[6].getContents().trim());
                            }
                        }
                        if (row.length > 7)
                        {
                            if (row[7].getType().equals(CellType.EMPTY))
                            {
                                utacpop3WipStatus.setQty(null);
                            }
                            else
                            {
                                utacpop3WipStatus.setQty(row[7].getContents().trim());
                            }
                        }
                        if (row.length > 8)
                        {
                            if (row[8].getType().equals(CellType.EMPTY))
                            {
                                utacpop3WipStatus.setSubQty(null);
                            }
                            else
                            {
                                utacpop3WipStatus.setSubQty(row[8].getContents().trim());
                            }
                        }
                        if (row.length > 9)
                        {
                            if (row[9].getType().equals(CellType.EMPTY))
                            {
                                utacpop3WipStatus.setStageAge(null);
                            }
                            else
                            {
                                utacpop3WipStatus.setStageAge(row[9].getContents().trim());
                            }
                        }
                        if (row.length > 10)
                        {
                            if (row[10].getType().equals(CellType.EMPTY))
                            {
                                utacpop3WipStatus.setLotAge(null);
                            }
                            else
                            {
                                utacpop3WipStatus.setLotAge(row[10].getContents().trim());
                            }
                        }
                        if (row.length > 11)
                        {
                            if (row[11].getType().equals(CellType.EMPTY))
                            {
                                utacpop3WipStatus.setLotType(null);
                            }
                            else
                            {
                                utacpop3WipStatus.setLotType(row[11].getContents().trim());
                            }
                        }
                        if (row.length > 12)
                        {
                            if (row[12].getType().equals(CellType.EMPTY))
                            {
                                utacpop3WipStatus.setState(null);
                            }
                            else
                            {
                                utacpop3WipStatus.setState(row[12].getContents().trim());
                            }
                        }
                        if (row.length > 13)
                        {
                            if (row[13].getType().equals(CellType.EMPTY))
                            {
                                utacpop3WipStatus.setHoldReason(null);
                            }
                            else
                            {
                                utacpop3WipStatus.setHoldReason(row[13].getContents().trim());
                            }
                        }
                        if (row.length > 14)
                        {
                            if (row[14].getType().equals(CellType.EMPTY))
                            {
                                utacpop3WipStatus.setWaferLotNo(null);
                            }
                            else
                            {
                                utacpop3WipStatus.setWaferLotNo(row[14].getContents().trim());
                            }
                        }
                        if (row.length > 15)
                        {
                            if (row[15].getType().equals(CellType.EMPTY))
                            {
                                utacpop3WipStatus.setWaferQty(null);
                            }
                            else
                            {
                                utacpop3WipStatus.setWaferQty(row[15].getContents().trim());
                            }
                        }
                        if (row.length > 16)
                        {
                            if (row[16].getType().equals(CellType.EMPTY))
                            {
                                utacpop3WipStatus.setDateCode(null);
                            }
                            else
                            {
                                utacpop3WipStatus.setDateCode(row[16].getContents().trim());
                            }
                        }
                        if ( utacpop3WipStatus.getCustomerDevice() != null && utacpop3WipStatus.getCustomerDevice().length() > 0 )
                        {
                            utacWipStatusList.add( utacpop3WipStatus );
                        }
                    }
                }
            }
        }
        catch (Exception ex)
        {
            ex.printStackTrace();
            throw ex;
        }

        return utacWipStatusList;
    }

    private void getSheetDetails(ArrayList lotstatusList) throws Exception
    {
        UTACPOP3WipStatus wipStatus = null;
        InventoryLot dbinventoryLot = null;
        InventoryLot wsinventoryLot = null;
        InventoryLot markPackLot    = null;
        AssemblyLot assemblyLot     = null;
        FabSortLot sortLot          = null;
        TestLot testLot             = null;
        String partNumber           = null;
        String lotNumber            = null;
        String fabLotNumber         = null;
        String dateIn               = null;
        String lotCategory          = null;
        String lotStatus            = null;

        if ( lotstatusList == null || lotstatusList.size() <= 0 )
        {
            return;
        }

        try
        {
            Iterator iterator = lotstatusList.iterator();

            Calendar cal = Calendar.getInstance();
            dateIn = StringUtils.formatDateToString(cal.getTime());

            while (iterator.hasNext())
            {
                wipStatus = (UTACPOP3WipStatus) iterator.next();

                fabLotNumber = wipStatus.getCustomerLotId();
                partNumber = wipStatus.getCustomerDevice();
                lotNumber  = wipStatus.getUtacLotId();
                if (lotNumber == null )
                {
                    lotNumber = fabLotNumber;
                }
                if ( wipStatus.getLotType() != null && "EL".equalsIgnoreCase( wipStatus.getLotType() ) )
                {
                    lotCategory = LOT_CATEGORY_TYPE_ENGG;
                }
                else
                {
                    lotCategory = LOT_CATEGORY_TYPE_PROD;
                }
                if( wipStatus.getState() != null && ( "WAIT".equalsIgnoreCase( wipStatus.getLotType())
                        || "HELD".equalsIgnoreCase( wipStatus.getLotType()) || wipStatus.getLotType().indexOf("HLD") != -1 ) )
                {
                    lotStatus = LOT_STATUS_HOLD;
                }
                else
                {
                    lotStatus = LOT_STATUS_RUNNING;
                }

                if ( lotNumber != null && lotNumber.trim().length() > 0 && partNumber != null && partNumber.trim().length() > 0)
                {
                    //WAFER STORE LOTS
                    if ( wipStatus.getStageDesc() != null && "Wafer Bank".equalsIgnoreCase( wipStatus.getStageDesc() ) )
                    {
                        if ( partNumber.indexOf("_WS") != -1 )
                        {
                            partNumber = partNumber.substring( 0,partNumber.indexOf("_WS") ) ;
                        }

                        log("In Wafer Bank " + lotNumber );
                        wsinventoryLot = (InventoryLot) m_waferBankDataMap.get( partNumber + fabLotNumber);
                        if ( wsinventoryLot == null )
                        {
                            wsinventoryLot = new InventoryLot(VENDOR_NAME_UTAC_SING, LOCATION_TYPE_WAFER_STORE );
                            m_waferBankDataMap.put( partNumber + fabLotNumber, wsinventoryLot );
                        }
                        int quantity = 0;
                        if (wsinventoryLot.getWaferQtyIn() != null)
                        {
                            try
                            {
                                quantity = Integer.parseInt(wsinventoryLot.getWaferQtyIn());
                            }
                            catch (NumberFormatException ignore) {
                            }
                            try
                            {
                                quantity += Integer.parseInt(wipStatus.getQty() );
                            }
                            catch (NumberFormatException nfe) {
                            }
                        }
                        else
                        {
                            try
                            {
                                quantity = Integer.parseInt( wipStatus.getQty() );
                            }
                            catch (NumberFormatException ignore) {
                            }
                        }

                        if ( quantity > 0 )
                        {
                            wsinventoryLot.setLotNumber( fabLotNumber );
                            wsinventoryLot.setPartNumber( partNumber );
                            wsinventoryLot.setStatus( lotStatus );
                            wsinventoryLot.setFabLotNumber( fabLotNumber );
                            wsinventoryLot.setCategory( lotCategory );
                            wsinventoryLot.setWaferQtyIn( quantity +"" );
                            wsinventoryLot.setHoldReason( wipStatus.getHoldReason() );
                            wsinventoryLot.setDateCode( wipStatus.getDateCode() );
                        }
                    }

                    //SORT LOTS
                    if (  wipStatus.getStage() != null && ( wipStatus.getStage().indexOf("W050") != -1
                          || wipStatus.getStage().indexOf("W100") != -1 || wipStatus.getStage().indexOf("W998") != -1
                          || wipStatus.getStage().indexOf("W600") != -1 || wipStatus.getStage().indexOf("W760") != -1
                          || wipStatus.getStage().indexOf("P190") != -1 )  )
                    {
                        log( "In Sort " + lotNumber );
                        sortLot = (FabSortLot)m_sortDataMap.get( partNumber + fabLotNumber );
                        if ( sortLot == null )
                        {
                            sortLot = new FabSortLot(VENDOR_NAME_UTAC_SING, WIP_TYPE_SORT );
                            m_sortDataMap.put( partNumber +  fabLotNumber , sortLot);
                        }
                        int quantity = 0;
                        if (sortLot.getQtyIn() != null)
                        {
                            try
                            {
                                quantity = Integer.parseInt(sortLot.getQtyIn());
                            }
                            catch (NumberFormatException ignore) {
                            }
                            try
                            {
                                quantity += Integer.parseInt(wipStatus.getQty());
                            }
                            catch (NumberFormatException nfe) {
                            }
                        }
                        else
                        {
                            try
                            {
                                quantity = Integer.parseInt(wipStatus.getQty());
                            }
                            catch (NumberFormatException ignore) {
                            }
                        }
                        if ( quantity > 0 )
                        {
                            sortLot.setQtyIn( quantity +"" );
                            sortLot.setStage( wipStatus.getStageDesc() );
                            sortLot.setCustProduct( partNumber);
                            sortLot.setLotId( fabLotNumber);
                            sortLot.setFabLotNumber( fabLotNumber );
                            sortLot.setDateIn( dateIn );
                            sortLot.setLotStatus( lotStatus );
                            sortLot.setHoldReason( wipStatus.getHoldReason() );
                            sortLot.setLotCategory( lotCategory );
                            sortLot.setDateCode( wipStatus.getDateCode() );
                        }
                    }

                    //DIE BANK LOTS
                    if ( wipStatus.getStageDesc() != null && wipStatus.getStageDesc().equalsIgnoreCase("Die Bank")  )
                    {
                        int bomPocessstep_id = NewAdaptorDAO.getBOMProcessStepId( partNumber , WIP_TYPE_ASSEMBLY );

                        if ( bomPocessstep_id > 0 )
                        {
                            log ( "In BOM Die bank--> Assembly Bom " + lotNumber );
                            assemblyLot = (AssemblyLot) m_assyWipDataMap.get( partNumber + lotNumber + fabLotNumber );
                            if (assemblyLot == null)
                            {
                                assemblyLot = new AssemblyLot( VENDOR_NAME_UTAC_SING );
                                m_assyWipDataMap.put( partNumber + lotNumber + fabLotNumber, assemblyLot);
                            }
                            int quantity = 0;
                            if (assemblyLot.getQtyIn() != null)
                            {
                                try
                                {
                                    quantity = Integer.parseInt(assemblyLot.getQtyIn());
                                }
                                catch (NumberFormatException ignore) {
                                }
                                try
                                {
                                    quantity += Integer.parseInt(wipStatus.getQty());
                                }
                                catch (NumberFormatException nfe) {
                                }
                            }
                            else
                            {
                                try
                                {
                                    quantity = Integer.parseInt(wipStatus.getQty());
                                }
                                catch (NumberFormatException ignore) {
                                }
                            }
                            assemblyLot.setLotNumber( lotNumber );
                            assemblyLot.setDeviceName( partNumber );
                            assemblyLot.setStatus(lotStatus);
                            assemblyLot.setFabLotNumber( fabLotNumber );
                            assemblyLot.setCategory( lotCategory );
                            assemblyLot.setDateIn( dateIn ) ;
                            assemblyLot.setQtyIn( quantity +"" );
                            assemblyLot.setProcessType( WIP_TYPE_ASSEMBLY );
                            assemblyLot.setHoldReason( wipStatus.getHoldReason() );
                            assemblyLot.setStage( wipStatus.getStageDesc() );
                            assemblyLot.setDateCode( wipStatus.getDateCode());
                        }
                        else
                        {
                             log ( "In Die bank " + lotNumber );

                             dbinventoryLot = (InventoryLot) m_dieBankDataMap.get( partNumber + lotNumber + fabLotNumber );
                             if ( dbinventoryLot == null )
                             {
                                 dbinventoryLot = new InventoryLot(VENDOR_NAME_UTAC_SING, INVENTORY_TYPE_DIE_BANK );
                                 m_dieBankDataMap.put( partNumber + lotNumber + fabLotNumber, dbinventoryLot );
                             }

                            int quantity = 0;
                            if (dbinventoryLot.getQtyIn() != null)
                            {
                                try
                                {
                                    quantity = Integer.parseInt(dbinventoryLot.getQtyIn());
                                }
                                catch (NumberFormatException ignore) {
                                }
                                try
                                {
                                    quantity += Integer.parseInt(wipStatus.getQty());
                                }
                                catch (NumberFormatException nfe) {
                                }
                            }
                            else
                            {
                                try
                                {
                                    quantity = Integer.parseInt(wipStatus.getQty());
                                }
                                catch (NumberFormatException ignore) {
                                }
                            }

                            if ( quantity >0 )
                            {
                                dbinventoryLot.setLotNumber( lotNumber );
                                dbinventoryLot.setPartNumber( partNumber );
                                dbinventoryLot.setStatus( lotStatus );
                                dbinventoryLot.setFabLotNumber( fabLotNumber );
                                dbinventoryLot.setCategory( lotCategory );
                                dbinventoryLot.setDateIn( dateIn );
                                dbinventoryLot.setQtyIn( quantity +"" );
                                dbinventoryLot.setHoldReason( wipStatus.getHoldReason() );
                                dbinventoryLot.setDateCode( wipStatus.getDateCode() );
                            }
                        }
                    }
                    //ASSEMBLY
                    if( wipStatus.getStage() != null &&( wipStatus.getStage().indexOf("A090") != -1 || wipStatus.getStage().indexOf("A100") != -1
                          || wipStatus.getStage().indexOf("A125") != -1 || wipStatus.getStage().indexOf("A200") != -1 || wipStatus.getStage().indexOf("A300") != -1
                          || wipStatus.getStage().indexOf("A380") != -1 || wipStatus.getStage().indexOf("A450") != -1 || wipStatus.getStage().indexOf("A451") != -1
                          || wipStatus.getStage().indexOf("A480") != -1 || wipStatus.getStage().indexOf("A490") != -1 || wipStatus.getStage().indexOf("A491") != -1
                          || wipStatus.getStage().indexOf("A500") != -1 || wipStatus.getStage().indexOf("A700") != -1 || wipStatus.getStage().indexOf("A900") != -1
                          || wipStatus.getStage().indexOf("A990") != -1 || wipStatus.getStage().indexOf("F150") != -1 || wipStatus.getStage().indexOf("F160") != -1
                          || wipStatus.getStage().indexOf("F200") != -1 || wipStatus.getStage().indexOf("F600") != -1 || wipStatus.getStage().indexOf("F625") != -1
                          || wipStatus.getStage().indexOf("F700") != -1 || wipStatus.getStage().indexOf("F800") != -1 || wipStatus.getStage().indexOf("F900") != -1 )  )
                    {
                        log( "In Assembly " + lotNumber ) ;

                        assemblyLot = (AssemblyLot) m_assyWipDataMap.get( partNumber + lotNumber + fabLotNumber );
                        if (assemblyLot == null)
                        {
                            assemblyLot = new AssemblyLot( VENDOR_NAME_UTAC_SING );
                            m_assyWipDataMap.put( partNumber + lotNumber + fabLotNumber, assemblyLot);
                        }

                        int quantity = 0;
                        if (assemblyLot.getQtyIn() != null)
                        {
                            try
                            {
                                quantity = Integer.parseInt(assemblyLot.getQtyIn());
                            }
                            catch (NumberFormatException ignore) {
                            }
                            try
                            {
                                quantity += Integer.parseInt(wipStatus.getQty());
                            }
                            catch (NumberFormatException nfe) {
                            }
                        }
                        else
                        {
                            try
                            {
                                quantity = Integer.parseInt(wipStatus.getQty());
                            }
                            catch (NumberFormatException ignore) {
                            }
                        }
                        if ( quantity > 0 )
                        {
                            assemblyLot.setDeviceName(partNumber);
                            assemblyLot.setLotNumber(lotNumber);
                            assemblyLot.setQtyIn(quantity + "");
                            assemblyLot.setDateIn(dateIn);
                            assemblyLot.setFabLotNumber(fabLotNumber);
                            assemblyLot.setStatus(lotStatus );
                            assemblyLot.setHoldReason( wipStatus.getHoldReason() );
                            assemblyLot.setCategory(lotCategory);
                            assemblyLot.setStage( wipStatus.getStageDesc() );
                            assemblyLot.setDateCode( wipStatus.getDateCode() );
                        }
                    }

                    //TEST
                    if( wipStatus.getStage() != null &&( wipStatus.getStage().indexOf("P400") != -1 || wipStatus.getStage().indexOf("T100") != -1
                          || wipStatus.getStage().indexOf("T700") != -1 || wipStatus.getStage().indexOf("T998") != -1 ) )
                    {
                        log( "In Test " + lotNumber );

                        testLot = (TestLot) m_ftDataMap.get( partNumber + lotNumber + fabLotNumber);

                        if (testLot == null)
                        {
                            testLot = new TestLot(VENDOR_NAME_UTAC_SING );
                            m_ftDataMap.put( partNumber + lotNumber + fabLotNumber, testLot);
                        }

                        int quantity = 0;
                        if (testLot.getQtyIn() != null)
                        {
                            try
                            {
                                quantity = Integer.parseInt(testLot.getQtyIn());
                            }
                            catch (NumberFormatException ignore) {
                            }
                            try
                            {
                                quantity += Integer.parseInt(wipStatus.getQty());
                            }
                            catch (NumberFormatException nfe) {
                            }
                        }
                        else
                        {
                            try
                            {
                                quantity = Integer.parseInt(wipStatus.getQty());
                            }
                            catch (NumberFormatException ignore) {
                            }
                        }
                        if ( quantity > 0 )
                        {
                            testLot.setPartNumber(partNumber);
                            testLot.setLotNumber(lotNumber);
                            testLot.setQtyIn(quantity + "");
                            testLot.setDateIn(dateIn);
                            testLot.setFabLotNumber(fabLotNumber);
                            testLot.setStatus(lotStatus);
                            testLot.setHoldReason( wipStatus.getHoldReason() );
                            testLot.setPsType(LOCATION_TYPE_FT);
                            testLot.setCategory(lotCategory );
                            testLot.setStage( wipStatus.getStageDesc() );
                            testLot.setDateCode( wipStatus.getDateCode() );
                        }

                    }

                    // MARK & PACK
                    if( wipStatus.getStage() != null &&( wipStatus.getStage().indexOf("E000") != -1 || wipStatus.getStage().indexOf("E220") != -1
                          || wipStatus.getStage().indexOf("E330") != -1 || wipStatus.getStage().indexOf("E400") != -1 || wipStatus.getStage().indexOf("E900") != -1
                          || wipStatus.getStage().indexOf("P600") != -1 ) )
                    {
                        log( "In Mark & Pack " + lotNumber );

                        markPackLot = (InventoryLot) m_markPackDataMap.get( partNumber + lotNumber + fabLotNumber);

                        if (markPackLot == null)
                        {
                            markPackLot = new InventoryLot(VENDOR_NAME_UTAC_SING, LOCATION_TYPE_MARK_AND_PACK );
                            m_markPackDataMap.put( partNumber + lotNumber + fabLotNumber, markPackLot);
                        }
                        int quantity = 0;
                        if (markPackLot.getQtyIn() != null)
                        {
                            try
                            {
                                quantity = Integer.parseInt(markPackLot.getQtyIn());
                            }
                            catch (NumberFormatException ignore) {
                            }
                            try
                            {
                                quantity += Integer.parseInt(wipStatus.getQty());
                            }
                            catch (NumberFormatException nfe) {
                            }
                        }
                        else
                        {
                            try
                            {
                                quantity = Integer.parseInt(wipStatus.getQty());
                            }
                            catch (NumberFormatException ignore) {
                            }
                        }

                        if ( quantity > 0 )
                        {
                            markPackLot.setLotNumber( lotNumber );
                            markPackLot.setPartNumber( partNumber );
                            markPackLot.setStatus( lotStatus );
                            markPackLot.setFabLotNumber( fabLotNumber );
                            markPackLot.setCategory( lotCategory );
                            markPackLot.setStage( wipStatus.getStageDesc() );
                            markPackLot.setDateIn( dateIn );
                            markPackLot.setQtyIn( quantity +"");
                            markPackLot.setHoldReason( wipStatus.getHoldReason() );
                            markPackLot.setDateCode( wipStatus.getDateCode() );
                        }
                   }
                }
            }
        }
        catch( Exception e)
        {
            System.out.println("ERROR!! ERROR!! While parsing the UTAC Wip Details........" + e );
            e.printStackTrace();
            throw e;
        }
    }

   private static void log( String message )
    {

        if ( message == null ) return;

        if ( m_parserLogger != null )
        {
            synchronized ( m_parserLogger )
            {
                m_parserLogger.logDebug( message + LINE_SEPARATOR );
            }
        }
        else
        {
            System.out.println( message );
        }
    }
}
